package com.gitee.wsl.common.ui.ext.brush


import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.runtime.Composable
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shader
import androidx.compose.ui.graphics.ShaderBrush

/**
 * A complete blend mix sweep between two colors, ends in the started color for a clean blended sweep
 * @param start the starting color color
 * @param end the ending color
 * @return a new [Brush] for use in composables
 */
fun Brush.Companion.sweepBlend(start: Color, end: Color): Brush {
    return Brush.sweepGradient(listOf(start, end, end, start))
}

/**
 * A complete blend mix sweep between two colors, ends in the started color for a clean blended sweep
 * @param colors list of colors to sweep
 * @return a new [Brush] for use in composables
 */
fun Brush.Companion.sweepBlend(colors: List<Color>): Brush {
    val blendFriendlyList = mutableListOf<Color>()
    blendFriendlyList.addAll(colors)
    val finalColor = colors[0]
    blendFriendlyList.add(finalColor)
    return Brush.sweepGradient(blendFriendlyList)
}

fun Brush.Companion.shader(shader: Shader) = ShaderBrush(shader)

@Composable
fun Brush.Companion.shimmerLoading(
    duration: Int = 2000,
    color: Color = Color.LightGray,
): Brush {
    val gradient = listOf(
        color.copy(alpha = 0.8f),
        color.copy(alpha = 0.35f),
        color.copy(alpha = 0.8f),
    )

    val transition = rememberInfiniteTransition()
    val translateAnimation = transition.animateFloat(
        initialValue = 0f,
        targetValue = 1000f,
        animationSpec = infiniteRepeatable(
            animation = tween(
                durationMillis = duration,
                easing = FastOutLinearInEasing
            )
        )
    )

    return Brush.linearGradient(
        colors = gradient,
        start = Offset(250f, 250f),
        end = Offset(
            x = translateAnimation.value,
            y = translateAnimation.value
        )
    )
}